/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgt.onlinetest.controller;

import java.text.ParseException;
import java.util.Date;
import sgt.onlinetest.entity.*;
import sgt.onlinetest.hibernatedao.HibernateDAOFactory;
import sgt.onlinetest.util.Converter;

/**
 *
 * @author Administrator
 */
public class SampleInstaller {

    private static boolean sampleInstalled = false;

    public static synchronized void installSample() throws ParseException {
        if (!sampleInstalled) {
            System.out.println("Sample is installed already");
            sampleInstalled = true;
            internalInstallSample();
        }
    }

    private static void internalInstallSample() throws ParseException {
        HibernateDAOFactory factory = new HibernateDAOFactory();

        //create roles
        Role[] roles = {new Role("admin"), new Role("test-maker"), new Role("test-proctor")};
        factory.getRoleDAO().save(roles);
        //create users
        User[] users = {new User("phuongnd08", "Nguyen Duc Phuong", "123456", roles[0], roles[1], roles[2]), new User("ngocph06", "Phan Huu Ngoc", "123456", roles[1], roles[2]), new User("truongnm04", "Nguyen Minh Truong", "123456", roles[2])};

        factory.getUserDAO().save(users);

        //create question categories
        QuestionCategory root = new QuestionCategory("root", new QuestionCategory("Reading"), new QuestionCategory("Grammar"), new QuestionCategory("Listening"));
        factory.getQuestionCategoryDAO().save(root);
        //create questions
        Question[] questions = {
            new Question("What is the correct form of verb for She+BE?",
            root.getChildren().get(0),
            QuestionType.MULTIPLE_CHOICE,
            new QuestionOption("Am", false),
            new QuestionOption("Are", false),
            new QuestionOption("Is", true)),
            new Question("What is the best prep for be interested+?",
            root.getChildren().get(0),
            QuestionType.MULTIPLE_CHOICE,
            new QuestionOption("On", false),
            new QuestionOption("About", false),
            new QuestionOption("In", true)),
            new Question("I+BE, BE should be divided as...",
            root.getChildren().get(0),
            QuestionType.MULTIPLE_CHOICE,
            new QuestionOption("Am", true),
            new QuestionOption("Are", false),
            new QuestionOption("Is", false)),
            new Question("If I...you, I would not do so",
            root.getChildren().get(0),
            QuestionType.MULTIPLE_CHOICE,
            new QuestionOption("am", false),
            new QuestionOption("are", false),
            new QuestionOption("was", false),
            new QuestionOption("were", true)),
            new Question("beautiful is nearest to...",
            root.getChildren().get(1),
            QuestionType.MULTIPLE_CHOICE,
            new QuestionOption("great", false),
            new QuestionOption("cool", false),
            new QuestionOption("pretty", true),
            new QuestionOption("fine", false))
        };

        factory.getQuestionDAO().save(questions);

        TestSession[] testSessions = {
            new TestSession("Reading I - Midterm",
            new Test("phuongnd08", "1256",
            questions[0],
            questions[1],
            questions[2]),
            new Test("ngocph06", "1234",
            questions[2],
            questions[1],
            questions[0])),
            new TestSession("Reading II - Final",
            new Test("phuongnd08", "6543",
            questions[3],
            questions[4]),
            new Test("ngocph07", "7654",
            questions[4],
            questions[3]))
        };
        Test t0 = testSessions[0].getTests().get(0);
        t0.setStartTime(Converter.toDate("2009/12/12 8:30", "yyyy/MM/dd hh:mm"));
        t0.setEndTime(Converter.toDate("2009/12/12 10:30", "yyyy/MM/dd hh:mm"));
        t0.setFinished(true);
        t0.setTotalScore(98);
        t0.getTestItems().get(0).setAnswer("1");
        t0.getTestItems().get(1).setAnswer("2");
        t0.getTestItems().get(2).setAnswer("1");
        
        factory.getTestSessionDAO().save(testSessions);

    }
}
